<%--
  Created by IntelliJ IDEA.
  User: asus
  Date: 2018/3/7
  Time: 15:04
  To change this template use File | Settings | File Templates.
--%>
<%@page pageEncoding="UTF-8" %>
<!doctype html>
<html lang="zh-cmn">
<head>
    <%@include file="/WEB-INF/jsp/comm/head.jsp"%>
</head>
<body>
<div class="container">
    <div id="toolbar">
        <button type="button" class="btn btn-success" onclick="prepareGrant()"><i class="glyphicon glyphicon-user"></i> 角色授权</button>
    </div>
    <table id="roleTb"
           data-toggle="table"
           data-url="${ctx}/sys/role/search"
           data-striped="true"
           data-toolbar="#toolbar"
           data-click-to-selec="true">
        <thead>
        <tr>
            <th data-radio="true"></th>
            <th data-field="id">编号</th>
            <th data-field="roleName">名称</th>
            <th data-field="isStart" data-formatter="forStart">状态（启用/未启用）</th>
            <th data-field="createdBy">修改人</th>
            <th data-field="lastUpdateTime">修改时间</th>
        </tr>
        </thead>
    </table>
    <!---授权对话框-->
    <div id="grant">
        <div id="tree"></div>
    </div>
</div>

<%@include file="/WEB-INF/jsp/comm/foot.jsp"%>
<script>
    function forStart(value,row){
        return value==1?"<input type='checkbox' name='isStart' checked=checked />":"<input type='checkbox' name='isStart'/>";
    }
    /*实现级联勾选开始*/
    var nodeCheckedSilent = false;
    function nodeChecked (event, node){
        if(nodeCheckedSilent){
            return;
        }
        nodeCheckedSilent = true;
        checkAllParent(node);
        checkAllSon(node);
        nodeCheckedSilent = false;
    }

    var nodeUncheckedSilent = false;
    function nodeUnchecked  (event, node){
        if(nodeUncheckedSilent)
            return;
        nodeUncheckedSilent = true;
        uncheckAllParent(node);
        uncheckAllSon(node);
        nodeUncheckedSilent = false;
    }

    //选中全部父节点
    function checkAllParent(node){
        $('#tree').treeview('checkNode',node.nodeId,{silent:true});
        var parentNode = $('#tree').treeview('getParent',node.nodeId);
        if(!("nodeId" in parentNode)){
            return;
        }else{
            checkAllParent(parentNode);
        }
    }
    //取消全部父节点
    function uncheckAllParent(node){
        $('#tree').treeview('uncheckNode',node.nodeId,{silent:true});
        var siblings = $('#tree').treeview('getSiblings', node.nodeId);
        var parentNode = $('#tree').treeview('getParent',node.nodeId);
        if(!("nodeId" in parentNode)) {
            return;
        }
        var isAllUnchecked = true;  //是否全部没选中
        for(var i in siblings){
            if(siblings[i].state.checked){
                isAllUnchecked=false;
                break;
            }
        }
        if(isAllUnchecked){
            uncheckAllParent(parentNode);
        }

    }

    //级联选中所有子节点
    function checkAllSon(node){
        $('#tree').treeview('checkNode',node.nodeId,{silent:true});
        if(node.nodes!=null&&node.nodes.length>0){
            for(var i in node.nodes){
                checkAllSon(node.nodes[i]);
            }
        }
    }
    //级联取消所有子节点
    function uncheckAllSon(node){
        $('#tree').treeview('uncheckNode',node.nodeId,{silent:true});
        if(node.nodes!=null&&node.nodes.length>0){
            for(var i in node.nodes){
                uncheckAllSon(node.nodes[i]);
            }
        }
    }
    /*实现级联勾选结束*/
    function fmtState(value,row){
        return value==1?"启用":"禁用";
    }
    $(document).ready(function(){
        $("#grant").dialog({
            title:'角色授权',
            btnName:'确定',
            btnBind:function(){
                var rows=$("#roleTb").bootstrapTable("getSelections");
                var params="roleId="+rows[0].id;
                var arr=$("#tree").treeview("getChecked");
                $(arr).each(function(){
                    params=params+"&mids="+this.id;
                });
                $.post(ctx+"sys/permission/grant",params,function(msg){
                    toastr.warning(msg);
                    $("#grant").dialog("close");
                    $("#roleTb").bootstrapTable("uncheckAll");
                },'text');


            }
        });
    });
    function prepareGrant(){
        var rows=$("#roleTb").bootstrapTable("getSelections");
        if(rows.length==0){
            swal({
                title:"信息提示",
                text:"请选择一项待授权！",
                type:"warning",
                confirmButtonText:"确定"
            });
            return;
        }
        var url=ctx+"sys/permission/tree";
        var params={roleId:rows[0].id};
        $.getJSON(url,params,function(arr){
            $("#tree").treeview({
                data:arr,
                multiSelect:false,
                showCheckbox:true,
                onNodeChecked:nodeChecked ,
                onNodeUnchecked:nodeUnchecked
            });

            $("#grant").dialog("open");
        });
    }
</script>
</body>
</html>
